<template> 
    <div class="app-container">
        <el-card class="operate-container" shadow="never">
            <i class="el-icon-tickets"></i>
            <span>资源位</span><span  class="warm-tip">(banner图下的4个图片；按照排序字段排列)</span>
            <el-button class="btn-add" @click="addSubject()" size="medium" type="primary">
                添加
            </el-button>
        </el-card>
        <div class="choose-dept">
            <el-tabs v-model="activeDept" @tab-click="deptChange">
                <el-tab-pane v-for="item in depotList" :label="item.name" :name="item.name" :id="item.id" :key="item.id"></el-tab-pane>
            </el-tabs>
        </div>
        <div class="table-container">
            <el-table ref="subjectTable"
                      :data="list"
                      style="width: 100%"
                      @selection-change="handleSelectionChange"
                      v-loading="listLoading"
                      border>
                <el-table-column label="编号" width="100" align="center">
                    <template slot-scope="scope">{{scope.row.id}}</template>
                </el-table-column>
                <el-table-column label="标题" align="center">
                    <template slot-scope="scope">{{scope.row.name}}</template>
                </el-table-column>
                <el-table-column label="图片" align="center">
                  <template slot-scope="scope"><img style="height: 80px" :src="scope.row.picUrl"></template>
                </el-table-column>
                <el-table-column label="创建时间" align="center">
                    <template slot-scope="scope">{{scope.row.createTime|formatCreateTime}}</template>
                </el-table-column>
                <el-table-column label="排序" align="center">
                    <template slot-scope="scope">{{scope.row.sort}}</template>
                </el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleUpdate(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="batch-operate-container"></div>
        <!-- <div class="pagination-container">
            <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    layout="total, sizes,prev, pager, next,jumper"
                    :page-size="listQuery.pageSize"
                    :page-sizes="[5,10,15]"
                    :current-page.sync="listQuery.pageNum"
                    :total="total">
            </el-pagination>
        </div> -->
    </div>
</template>
<script>
    import {formatDate} from '@/utils/date';
    import {fetchHomeResourceList, updateHomeResource,deleteHomeResource,fetchRelatedProductList,updateResourceProduct,delRelatedProductList} from '@/api/home-resource'
    import {fetchList as fetchDeptList} from '@/api/depository'
    export default {
        name: 'subjectList',
        data() {
            return {
                operates: [
                    {
                        label: "显示品牌",
                        value: "showSubject"
                    },
                    {
                        label: "隐藏品牌",
                        value: "hideSubject"
                    }
                ],
                operateType: null,
                listQuery: {
                    keyword: null,
                    pageNum: 1,
                    pageSize: 10,
                    depotId:null,
                },
                list: null,
                total: null,
                listLoading: true,
                multipleSelection: [],
                activeDept:null,
                depotList:[],
            }
        },
        created() {
            this.getList();
        },
      filters: {
        formatCreateTime(time) {
          let date = new Date(time);
          return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        },

        formatStatus(value) {
          if (value === 1) {
            return '未开始';
          } else if (value === 2) {
            return '活动中';
          } else if (value === 3) {
            return '已结束';
          } else if (value === 4) {
            return '已失效';
          }
        },
      },
        methods: {
            getList() {
                let self = this;
                self.listLoading = true;
                fetchDeptList().then(({code,data})=>{
                    if(code ==200 && data){
                        self.depotList = data;
                        for(const item of data){
                            if(item.isDefault){
                                    self.activeDept= item.name;//页面一进来，首先选择显示默认仓库
                                    self.listQuery.depotId = item.id;
                                    break;
                                }
                            };
                        }
                        return data
                    }).then(respon=>{
                        fetchHomeResourceList(this.listQuery.depotId).then(response => {
                            if(response.code ==200 && response.data){
                                self.listLoading = false;
                                self.list = response.data;

                            }else{
                                self.listLoading = false;
                                self.list = [];
                            }
                            
                            // self.total = response.data.total;
                            // self.totalPage = response.data.pages;
                            // self.pageSize = response.data.size;
                        });
                    })
            },
          handleRecomChange(index, row) {
            let params = new URLSearchParams();
            params.append('ids', row.id);
            params.append('recommendStatus', row.recommendStatus);
            updateRecommendStatus(params).then(response => {
              this.$message({
              message: '修改成功',
              type: 'success',
              duration: 1000
            });
          });
          },
          handleShowChange(index, row) {
            let params = new URLSearchParams();
            params.append('ids', row.id);
            params.append('showStatus', row.showStatus);
            updateShowStatus(params).then(response => {
              this.$message({
              message: '修改成功',
              type: 'success',
              duration: 1000
            });
          });
          },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleUpdate(index, row) {
                this.$router.push({path: '/hds/updateResource', query: {id: row.id,depotId:this.listQuery.depotId}})
            },
            handleDelete(index, row) {
                this.$confirm('是否要删除该资源数据', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteHomeResource(row.id).then(response => {
                        this.$message({
                        message: '删除成功',
                        type: 'success',
                        duration: 1000
                    });
                    this.getList();
                 });
                });
            },
            getProductList(index, row) {
                console.log(index, row);
            },
            getProductCommentList(index, row) {
                console.log(index, row);
            },
            deptChange(tab, event){
                let self = this;
                let depotId = tab.$attrs.id;
                //self.listQuery.depotId = depotId;
                fetchHomeResourceList(depotId).then(response => {
                    if(response.code ==200 && response.data){
                        this.listLoading = false;
                        this.list = response.data.list;
                    }else{
                        self.listLoading = false;
                        self.list = [];
                    }
                })
            },

            handleSizeChange(val) {
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.listQuery.pageNum = val;
                this.getList();
            },
            searchSubjectList() {
                this.listQuery.pageNum = 1;
                this.getList();
            },
            handleBatchOperate() {
                console.log(this.multipleSelection);
                if (this.multipleSelection < 1) {
                    this.$message({
                        message: '请选择一条记录',
                        type: 'warning',
                        duration: 1000
                    });
                    return;
                }
                let showStatus = 0;
                if (this.operateType === 'showSubject') {
                    showStatus = 1;
                } else if (this.operateType === 'hideSubject') {
                    showStatus = 0;
                } else {
                    this.$message({
                        message: '请选择批量操作类型',
                        type: 'warning',
                        duration: 1000
                    });
                    return;
                }
                let ids = [];
                for (let i = 0; i < this.multipleSelection.length; i++) {
                    ids.push(this.multipleSelection[i].id);
                }
                let data = new URLSearchParams();
                data.append("ids", ids);
                data.append("showStatus", showStatus);
                updateShowStatus(data).then(response => {
                    this.getList();
                this.$message({
                    message: '修改成功',
                    type: 'success',
                    duration: 1000
                });
            });
            },
            addSubject() {
                this.$router.push({path: '/hds/addResource', query: {depotId: this.listQuery.depotId}})
            }
        }
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.btn-add{
    margin-top: -8px;
  }

</style>


